package com.qdairlines.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class TimeUtil {
	private static Logger logger = Logger.getLogger(TimeUtil.class);
	public static String getyyyyMMdd() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		return df.format(new Date());// new Date()为获取当前系统时间
	}

	public static String transDate(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		return df.format(date);// new Date()为获取当前系统时间
	}

	public static String getyyyyMMddHHmm() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 设置日期格式
		return df.format(new Date());// new Date()为获取当前系统时间
	}

	public static String getyyyyMMddHHmmss() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		return df.format(new Date());// new Date()为获取当前系统时间
	}
	
	public static String getyyyyMMddHHmmssSSS() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");// 设置日期格式
		return df.format(new Date());// new Date()为获取当前系统时间
	}

	public static boolean comparisonTime(String time1, String time2) {
		// 如果1比2大返回true time格式 yyyy-mm-dd hh:mi
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = sdf.parse(time1);
			date2 = sdf.parse(time2);
		} catch (ParseException e) {
			logger.error(TraceUtil.getTrace(e));
		}
		if (date1.getTime() >= date2.getTime()) {
			return true;
		}
		return false;

	}
	
	/**
	 * 如果1比2大返回true，否则返回false
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static boolean comparisonTimeyyyyMMddHHmmss(String time1, String time2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = sdf.parse(time1);
			date2 = sdf.parse(time2);
		} catch (ParseException e) {
			logger.error(TraceUtil.getTrace(e));
		}
		if (date1.getTime() > date2.getTime()) {
			return true;
		}
		return false;

	}

	public static String utcTime8(String time) {
		int endStd = time.indexOf(":");
		String mm = time.substring(endStd + 1);
		int h = Integer.parseInt(time.substring(0, endStd));
		int hh;
		String hhh;
		if (h - 8 >= 0) {
			hh = h - 8;
			if (hh < 10) {
				hhh = "0" + hh;
			} else {
				hhh = hh + "";
			}
		} else {
			hh = 24 + (h - 8);
			if (hh < 10) {
				hhh = "0" + hh;
			} else {
				hhh = hh + "";
			}
		}
		return hhh + mm;
	}


	public static String utcTime6(String time) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			calendar.setTime(sdf.parse(time));
			
			calendar.set(Calendar.HOUR,
					calendar.get(Calendar.HOUR) -6);
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
			return sdf2.format(calendar.getTime());
		} catch (ParseException e) {
			logger.error(TraceUtil.getTrace(e));
			return null;
		}
	}

	// 2016-08-01 6:20
	public static String utcTime830(String time) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			calendar.setTime(sdf.parse(time));
			
			calendar.set(Calendar.HOUR,
					calendar.get(Calendar.HOUR) -8);// 让日期加1
			calendar.set(Calendar.MINUTE,
					calendar.get(Calendar.MINUTE) -30);// 让日期加1
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
			return sdf2.format(calendar.getTime());
		} catch (ParseException e) {
			logger.error(TraceUtil.getTrace(e));
			return null;
		}
		
	}

	/**
	 * 判断字符串是否是合法的时间格式"yyyy-MM-dd HH:mm:ss"
	 * @param str
	 * @return
	 */
	public static boolean isValidDateyyyyMMddHHmmss(String str) {
		boolean convertSuccess = true;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//设置为严格验证模式，防止比如2007/02/29会被接受，并转换成2007/03/01  
		format.setLenient(false);
		try {
			format.parse(str);
		} catch (ParseException e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess = false;
		}
		return convertSuccess;
	}
	
	/**
	 * 判断字符串是否是合法的时间格式"yyyy-MM-dd"
	 * @param str
	 * @return
	 */
	public static boolean isValidDateyyyyMMdd(String str) {
		boolean convertSuccess = true;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		//设置为严格验证模式，防止比如2007/02/29会被接受，并转换成2007/03/01  
		format.setLenient(false);
		try {
			 format.parse(str);
		} catch (ParseException e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess = false;
		}
		return convertSuccess;
	}
}
